package com.whl.sm.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.system.OrgUserMapper;
import com.whl.sm.model.system.OrgUser;
import com.whl.sm.model.system.UserCpRole;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.service.system.UserCpRoleService;
import com.whl.sm.shiro.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-09-10
 */
@Service
public class OrgUserServiceImpl extends ServiceImpl<OrgUserMapper, OrgUser> implements OrgUserService {

    @Autowired
    private UserCpRoleService userCpRoleService;

    @Autowired
    HttpServletRequest currentRequest;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public int update(OrgUser orgUser) {
        int result;
        if (StringUtils.isNotBlank(orgUser.getId())){
            result = getBaseMapper().updateById(orgUser);
        }
        else {
            int count = getBaseMapper().selectCount(new QueryWrapper<OrgUser>()
                    .eq("account",orgUser.getAccount())
            );
            if (count > 0 ){
                return 0;
            }
            orgUser.setSource("Sys-Pc");
            result = getBaseMapper().insert(orgUser);
        }
        if (StringUtils.isNotBlank(orgUser.getRoleId())){
            UserCpRole userCpRole= new UserCpRole();
            userCpRole.setUserId(orgUser.getId());
            userCpRole.setRoleId(orgUser.getRoleId());
            result =  userCpRoleService.updateUserRole(userCpRole);
        }
        return result;
    }

    @Override
    public String getCurrentUserId() {
        String userId = null;
        String tokenHeader = currentRequest.getHeader("Authorization");//从头部获取JWT字符串信息
        if (Objects.nonNull(tokenHeader)){
            userId = JwtUtils.getUserId(tokenHeader);
        }
        return userId;
    }

    @Override
    public Result register(OrgUser orgUser) {
        try {
            List<OrgUser> hasList = getBaseMapper().selectList(new QueryWrapper<OrgUser>()
                    .eq("account",orgUser.getAccount())
                    .ne("is_del",1)
            );
            if (hasList.size()>0){
                return Result.build(444,"注册失败，注册账户已存在！");
            }
            orgUser.setSource("wx-register");
            update(orgUser);
        }catch (Exception e){
            log.error("账户注册失败---------->"+e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }





}
